package pro.survey.analyzer;

import java.util.List;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Bounds;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Tooltip;
import javafx.scene.text.Text;
import pro.survey.analyzer.exceptions.InvalidNumberItemsAndValues;

/**
 *
 * @author Adrian
 */
public class RenderCharts {

    /**
     *
     * @param title
     * @param items
     * @param values
     * @return
     * @throws Exception
     */
    public static PieChart createPieChart(String title, List<String> items, List<Integer> values) throws Exception {
        if (items.size() != values.size()) {
            throw new InvalidNumberItemsAndValues();
        }
        ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
        for (int i = 0; i < items.size(); i++) {
            pieChartData.add(new PieChart.Data(items.get(i), values.get(i)));
        }
        final PieChart pieChart = new PieChart(pieChartData);
        pieChart.setTitle(title);
        return pieChart;
    }

    /**
     *
     * @param title
     * @param xAxisLabel
     * @param yAxisLabel
     * @param items
     * @param values
     * @return
     * @throws Exception
     */
    public static BarChartWithNote createBarChart(String title, String xAxisLabel, String yAxisLabel, pro.survey.analyzer.data.Series series) {
        try {
            if (series.getItems().size() != series.getValues().size()) {
                throw new InvalidNumberItemsAndValues();
            }
        } catch (InvalidNumberItemsAndValues ex) {
            System.out.print(ex.getMessage());
        }
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();
        BarChartWithNote bc = new BarChartWithNote(xAxis, yAxis);
        bc.getChart().setTitle(title);
        xAxis.setLabel(xAxisLabel);
        yAxis.setLabel(yAxisLabel);

        XYChart.Series s = new XYChart.Series();
        for (int i = 0; i < series.getItems().size(); i++) {
            s.getData().add(new XYChart.Data(series.getItems().get(i), series.getValues().get(i)));
        }
        
        bc.getChart().getData().add(s);
        return bc;
    }

    /**
     *
     * @param title
     * @param xAxisLabel
     * @param yAxisLabel
     * @param items
     * @param values
     * @return
     * @throws Exception
     */
    public static BarChart createBarChartSeries(String title, String xAxisLabel, String yAxisLabel, List<pro.survey.analyzer.data.Series> seriess) throws InvalidNumberItemsAndValues {
        final CategoryAxis xAxis = new CategoryAxis();
        final NumberAxis yAxis = new NumberAxis();
        final BarChart<String, Number> bc = new BarChart<String, Number>(xAxis, yAxis);
        bc.setTitle(title);
        bc.setMinHeight(400);
        xAxis.setLabel(xAxisLabel);
        yAxis.setLabel(yAxisLabel);

        for (pro.survey.analyzer.data.Series s : seriess) {
            if (!s.validate()) {
                throw new InvalidNumberItemsAndValues();
            }
            XYChart.Series series = new XYChart.Series();
            series.setName(s.getTitle());
            for (int i = 0; i < s.getItems().size(); i++) {
                series.getData().add(new XYChart.Data(s.getItems().get(i), s.getValues().get(i)));
            }
            bc.getData().add(series);
        }
        return bc;
    }
}
